package com.yinhai.message.implement.im.adapter;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.util.MessageType;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.adapterManager.IAdapter;
import com.yinhai.message.core.event.IMessageEvent;
import com.yinhai.message.core.exception.ResultsHandle;
import com.yinhai.message.implement.im.event.ImMessageEvent;
import com.yinhai.message.implement.im.sender.ImMessageSender;

/**
 * Created by zhou on 2017/3/29.
 */

@Component("imAdapter")
public class ImAdapter implements IAdapter, ApplicationListener<ImMessageEvent>{

    private static final Logger LOGGER = LoggerFactory.getLogger(ImAdapter.class);

	@Resource(name = "resultsHandle")
	private ResultsHandle resultsHandle;
	
	@Resource(name = "imMessageSender")
	private ImMessageSender imMessageSender;

    @Override
    public String getSenderType(){
        return MessageType.IM;
    }

    @Override
    public IMessageEvent getEven(TMMessageVo vo){
        return new ImMessageEvent(vo);
    }

    @Async
    @Override
    public void onApplicationEvent(ImMessageEvent event) {
        LOGGER.debug("IM处理事件===========================");
        TMMessageVo messageSendVO = event.getMessageVo();
        String mgid = messageSendVO.getMessage().getMgid();
        List<TMUser> resuleUserList = imMessageSender.sendMessage(messageSendVO);
        resultsHandle.handleResults(resuleUserList,mgid,getSenderType());
    }

}
